﻿
using System;
using System.Collections.Generic;
using System.Text;

#if NETSTANDARD2_0
using Alipay.EasySDK.Factory;
using Alipay.EasySDK.Kernel;
using Alipay.EasySDK.Kernel.Util;
using Alipay.EasySDK.Marketing.OpenLife.Models;
using Alipay.EasySDK.Payment.FaceToFace.Models;
#endif

namespace PddHelper
{
    /// <summary>
    /// 支付宝帮助类
    /// </summary>
    public class AlipayHelper
    {

#if NETSTANDARD2_0

        #region 支付宝配置信息

        /// <summary>
        /// 支付宝配置信息
        /// </summary>
        public static Config config = new Config()
        {
            Protocol = "https",
            GatewayHost = "openapi.alipay.com",
            SignType = "RSA2",
            AppId = "2021004136622501",
            //应用私钥(支付宝公钥生成的RSA2私钥要转换下,不然异常,装换流程: 支付宝工具--左侧导航--第3项(格式转换)--私有放里面--点击转换),这里使用转换后的私钥
            MerchantPrivateKey = "MIIEowIBAAKCAQEA5yKd8nLNRy1preTOs8Kpw6ajtNGVDjsllkJd9MuK6abbT2seaJD9vaHGORgPuYpYpkN9eWzATc4KWMfEighLsSN1jRbBKfin62ztDO7iMDEVXed51voohHfTkbcok5n7ZGqkcAUifKa/VIg1JUC++v7M4MwA8kNt8JWJ+VHFv4Ma1bkIuXvPDa3bO1HZW1VIKguoQGRLaXnV6pHHR5JaK94ExAELLffBQIME+UGFGxwPpXtfTqXgiNuaeQYulhfdJNXoxVJgmFGmkR1QC2CCtiT19e6SwQmvZ3xSD2Tfn9BsB7RNa9ix0Sr8wxpgiBfO0Xmb+WETL8hS1BbUmDxQSQIDAQABAoIBAC4QOxQnhvupdEdAs2Ul5Xlli3is0Bzu8rW4cksoCX45T7nLKltCkh8pz3xvbCrZBIPWLXp2jviymGNLJMMxlgwuee3tW//GW4jSw4dxUEi0b4KwcBrE7jBARTzkGMUZyf0gN7dPfqkC/lyVbKoI9kbFYwVmAsZFY/KvirjScCuTT16iGGLPGLAAGWNpQDCUeXzuezbJW17ijfpMwb0KhpXFhi8IqKe9pJ6QyXWJepkqi97TVFSbGgdPnN8uXT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6O9q2SxMnCKpnj7rGtUKs4EH3waPRPZ4Ulo1pWZvJ8PM3zGE3uMLrJRb7U0X0eL1sXjUuuDQOLVCen7HA+iShCPSCaKG3FQe0JGG+HWf4wUkRtf6bsiYC+315SgZioK0ginfx3xvj6GQ6yeekqXVaSaF2LX3HGxACkvmw+74lGOZ/N8pZnNgu1Gfuko7El7IOrAi+3cQmlQSjZzw3PHdLurVR47bKYd54gmVSLgY9izOui+Y5CDHQ0Rr0VNvU9al0tSwK/i9GHArCEXPGHrX9HAB2Bxe0NbfVMGxYdTchGttQIDAQAB",

            //可设置异步通知接收服务地址（可选）
            NotifyUrl = "https://liepin-elhaokkbdv.cn-beijing.fcapp.run/ChongZhi/AlipayCallBack",

            //可设置AES密钥，调用AES加解密相关接口时需要（可选）
            EncryptKey = "ii1npZRd/1+tJ2mZzvyzog=="
        };
        #endregion

        #region 支付宝预支付

        /// <summary>
        /// 支付宝预支付
        /// </summary>
        /// <param name="subject">商品名称</param>
        /// <param name="outTradeNo">订单号</param>
        /// <param name="price">金额</param>
        /// <returns></returns>
        public static string GetAlipayCode(string subject, string outTradeNo, decimal price)
        {
            Factory.SetOptions(config);//1. 设置参数（全局只需设置一次）
            try
            {
                //price = Math.Ceiling(price);

                AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().PreCreate(subject, outTradeNo, price.ToString());// 2. 发起API调用（以创建当面付收款二维码）
                if (ResponseChecker.Success(response))// 3. 处理响应或异常
                {
                    return response.QrCode;
                }
                else
                {
                    Console.WriteLine("调用失败，原因：" + response.Msg + "，" + response.SubMsg);
                }
            }
            catch
            {
            }
            return "";
        }

        #endregion

#endif
    }
}
